package com.hxd.mall.utils.local

import android.annotation.SuppressLint
import android.text.TextUtils
import android.widget.EditText
import android.widget.TextView
import androidx.viewpager.widget.ViewPager
import com.hexin.locallibrary.banner.Banner
import com.hexin.locallibrary.banner.BannerConfig
import com.hexin.locallibrary.banner.listener.OnBannerListener
import com.hxd.mall.R
import com.hxd.mall.constant.HxdConstants.IntentParamsKeyDefine.key_web_url
import com.hxd.mall.constant.HxdConstantsRouterPath.Common.path_to_webview
import com.hxd.mall.module.product.home.active.ActiveTypeListAdapter
import com.hxd.mall.module.product.home.active.HomeActiveTypeListAdapter
import com.hxd.mall.module.product.home.bean.Resource
import com.hxd.mall.module.school.bean.ItemResource
import com.hxd.mall.utils.local.NavigationUtil.routerNavigation

/**
 * Create by yinzhengwei on 2019-09-25
 * @Function
 */

fun setBtnBackground(view: TextView, isEnable: Boolean) {
    view.isEnabled = isEnable
    view.setBackgroundResource(if (isEnable) R.mipmap.btn_normal else R.mipmap.btn_disabled)
}

fun setNameInfo(name: String, textView: EditText) {
    textView.setText(if (name.isNotEmpty()) "*${name.substring(1, name.length)}" else name)
    textView.setSelection(textView.text.length)
}

@SuppressLint("SetTextI18n")
fun setPhoneInfo(phone: String, textView: TextView) {
    textView.text = "${phone.substring(0, 3)} **** ${phone.substring(7)}"
}

//将手机号中的中划线、空格、括号去除
fun replaceContent(phone: String, vararg types: String) {
    types.forEach {
        if (phone.contains(it)) {
            phone.replace(it, "")
        }
    }
}

//将 +8613311029751、86、086这样的前缀过滤掉
fun replaceStartContent(phone: String, vararg types: String): String {
    var text = phone
    types.forEach {
        if (text.startsWith(it)) {
            text = text.substring(it.length)
        }
    }
    return text
}

fun isContainsPoint(textView: TextView) = textView.text.toString().contains("*")

//banner 初始化
fun initBannerView(banner: Banner, item: List<Resource>?, bannerStyle: Int? = BannerConfig.CIRCLE_INDICATOR, corner: Int? = 15, callback: (Int) -> Unit) {
    if (null == item) return
    val imageList = mutableListOf<String>()
    imageList.clear()
    item.forEach {
        imageList.add(it.pictUri ?: "")
    }
    initBanner(banner, imageList, OnBannerListener {
        if (imageList.isNotEmpty() && imageList.size >= it)
            if (!TextUtils.isEmpty(item[it].entryUri))
            routerNavigation(path_to_webview, key_web_url, item[it].entryUri?:"")
    }, object : ViewPager.OnPageChangeListener {
        override fun onPageScrollStateChanged(state: Int) {
        }

        override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
        }

        override fun onPageSelected(position: Int) {
            callback(position)
        }
    }, corner, bannerStyle)
}

fun setAdapterListener(adapter: HomeActiveTypeListAdapter, callback: (Int) -> Unit) {
    adapter.setOnItemClickListener { _, _, position ->
        val oldIndex = adapter.index

        callback(position)

        adapter.index = position
        adapter.notifyItemChanged(oldIndex)
        adapter.notifyItemChanged(position)

    }
}

fun setAdapterListener(adapter: ActiveTypeListAdapter, callback: (Int) -> Unit) {
    adapter.setOnItemClickListener { _, _, position ->
        val oldIndex = adapter.index

        callback(position)

        adapter.index = position
        adapter.notifyItemChanged(oldIndex)
        adapter.notifyItemChanged(position)

    }
}